package com.zhuoxin.dao.system;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.zhuoxin.bean.system.SysMenu;
import com.zhuoxin.bean.system.SysRole;
import com.zhuoxin.bean.system.SysRoleMenu;
import com.zhuoxin.bean.system.SysRoleMenuSO;
import com.zhuoxin.bean.system.SysUser;
import com.zhuoxin.dao.BaseDAOImpl;

public class SysRoleMenuDAOImpl extends BaseDAOImpl<SysRoleMenu, SysRoleMenuSO> implements SysRoleMenuDAO {

	@Override
	public String getHqlWhere(SysRoleMenuSO so) {
		StringBuffer where=new StringBuffer();
		
		if(so.getFlag()!=null){
			where.append(" and flag=:flag");
		}
		
		if(so.getStatus()!=null){
			where.append(" and status=:status");
		}
		
		if(so.getRole()!=null && so.getRole().getId()!=null){
			where.append(" and role.id=:roleId");
		}
		
		if(so.getMenu()!=null && so.getMenu().getId()!=null){
			where.append(" and menu.id=:menuId");
		}
		return where.toString();
	}

	@Override
	public void setCriteriaParams(Criteria criteria, SysRoleMenuSO so) {
		if(so.getRole()!=null){
			criteria.createAlias("role", "r");
			if(so.getRole().getId()!=null){
				criteria.add(Restrictions.eq("r.id", so.getRole().getId()));
			}
		}
		
		if(so.getMenu()!=null){
			criteria.createAlias("menu", "m");
			if(so.getMenu().getId()!=null){
				criteria.add(Restrictions.eq("m.id", so.getMenu().getId()));
			}
		}
	}

	@Override
	public void setQueryParams(Query query, SysRoleMenuSO so) {
		if(so.getFlag()!=null){
			query.setParameter("flag", so.getFlag());
		}
		
		if(so.getStatus()!=null){
			query.setParameter("status", so.getStatus());
		}
		
		if(so.getRole()!=null && so.getRole().getId()!=null){
			query.setParameter("roleId", so.getRole().getId());
		}
		
		if(so.getMenu()!=null && so.getMenu().getId()!=null){
			query.setParameter("menuId", so.getMenu().getId());
		}
		
	}

	@Override
	public void switchVO2PO(SysRoleMenu po, SysRoleMenuSO so) {
		if(so.getRole()!=null && so.getRole().getId()!=null){
			SysRole role=findById(SysRole.class, so.getRole().getId());
			po.setRole(role);
		}
		
		if(so.getMenu()!=null && so.getMenu().getId()!=null){
			SysMenu menu=findById(SysMenu.class, so.getMenu().getId());
			po.setMenu(menu);
		}
	}

	public void delete(SysRoleMenuSO so) {
		String hql="delete from SysRoleMenu where 1=1"+getHqlWhere(so);
		Session session=getHibernateTemplate().getSessionFactory().getCurrentSession();
		Query query=session.createQuery(hql);
		setQueryParams(query, so);
		query.executeUpdate();
	}
	
	@SuppressWarnings("unchecked")
	public List<SysMenu> findMenuByUser(SysUser user){
		List<SysMenu> menus=null;
		
		StringBuffer hql=new StringBuffer();
		hql.append("select id, chName, enName, href, sort, fkMenuId from sys_menu m");
		hql.append(" where exists(select fkMenuId from sys_role_menu rm");
		hql.append(" where exists (select fkRoleId from sys_role_user ru");
		hql.append(" where ru.fkUserId=:fkUserId and ru.fkRoleId=rm.fkRoleId)");
		hql.append(" and m.id=rm.fkMenuId) and m.flag=true and m.status=true");
		hql.append(" order by m.sort,m.id");
		
		Session session=getHibernateTemplate().getSessionFactory().getCurrentSession();
		Query query=session.createSQLQuery(hql.toString());
		query.setParameter("fkUserId", user.getId());
		List<Object[]> list=query.list();
		if(list!=null && list.size()>0){
			menus=new ArrayList<SysMenu>();
			for (Object[] objs : list) {
				SysMenu menu=new SysMenu();
				menu.setId(((BigInteger)objs[0]).longValue());
				menu.setChName((String)objs[1]);
				menu.setEnName((String)objs[2]);
				if(objs[3]!=null){
					menu.setHref((String)objs[3]);
				}
				if(objs[4]!=null){
					menu.setSort((Integer)objs[4]);
				}
				if(objs[5]!=null){
					SysMenu pm=new SysMenu();
					pm.setId(((BigInteger)objs[5]).longValue());
					menu.setMenu(pm);
				}
				menus.add(menu);
			}
		}
		return menus;
	}

}
